<?php
namespace App\Http\Controllers\Admin\Weixin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Admin\CoringService;
use App\Services\Admin\AlarmanswerService;
use App\Services\Admin\AnalysisService;
use App\Http\Requests\Admin\UserRequest;
use \App\Http\Controllers\Admin\BaseController;
use Illuminate\Support\Facades\Storage;
use DB;
use Mail;

/**
 * weixin app api端测试接口
 * Class Weixin
 * @package App\Http\Controllers\Admin\Weixin
 */
class WeixinController
{
    protected $service;
    protected $analysisservice;

    public function __construct(CoringService $service)
    {
        $this->service = $service;
    }
 
    public function index()
    {
    	$result = json_encode($this->service);
        return $result;
    }

    // wx.request报警历史信息请求路由接口
    public function alarminfo(AnalysisService $analysisservice) {
    	// $this->analysisservice = $analysisservice;
    	// $alarminfo = $this->analysisservice->ajaxData();
    	// 换用db方式来取数据
    	$alarminfo_all = DB::table('rivers_alarm_history')->get();
    	// 增加河道名称 河道负责人 负责人联系方式
    	foreach ($alarminfo_all as $alarminfo_all_item) {
    		$river_id = $alarminfo_all_item->river_id;
    		// 从rivers表中取出对应的河道名称
    		$river_name = DB::table('rivers')->where('river_id',$river_id)->value('river_name');
    		$alarminfo_all_item->river_name = $river_name;
    		// 从town_river中取出乡镇负责人信息
    		$user_id = DB::table('town_river')->where('river_id',$river_id)->value('town_manager_id');
    		//从users表中取出负责人的姓名
    		$user_name = DB::table('users')->where('id',$user_id)->value('name');
    		$alarminfo_all_item->manager_name = $user_name;
    		$user_email = DB::table('town_river')->where('river_id',$river_id)->value('town_manager_email');
    		$alarminfo_all_item->email = $user_email;
    	}
		$result = json_encode($alarminfo_all);    	
    	return $result;
    }

    // wx.request 微信API后端河道信息实时监测数据接口
    public function alltimeinfo() {
    	$result = $this->service->ajaxData();
    	return $result;
    }

    // wx.request获取报警音乐接口
    public function alarmmusic() {

    }

    // wx.request图片文件上传接口
    public function imgUpload(Request $request) {
	// 获取所有的上传图片，利用$_FILES进行获取
        $img_name_ext = $_FILES['file']['name'];
	$img = $request->file('file');
	$img_name = $request->input('name');
	$img_size = $_FILES['file']['size'];
	$img_type = $_FILES['file']['type'];
	//$path_res = $_FILES->storeAs($img_name_ext);
	$uploaddir = 'app/public/upload/';
	$uploadfile = $uploaddir.basename($img_name_ext);
	$timename = date('Y-m-d H:i:s',time());
	$res = $img->storeAs("Weixin",$img_name_ext);
	return $res;
	return $path_res;
		
    } 

    // wx.request用户注册接口
    public function register(Request $request) {
	$accountname = $request->input('accountname');
	$accountpsd = $request->input('accountpsd');
	$register_info = [];
	$register_info['username'] = $accountname;
	$register_info['password'] = $accountpsd;
	$register_info['register_time'] = date('Y-m-d H:i:s',time());
	$res = DB::table('weixin_users')->insert($register_info);
	if ($res) {
	    return response()->json(array(
			'res' => 1
		));
    	}else {
	    return response()->json(array(
                        'res' => 0
                ));  
	}
    }
    
    // wx.request post用户登录请求接口入口
    public function login(Request $request) {
	$accountname = $request->input('accountname');
	$accountpsd = $request->input('accountpsd');
	$res = DB::table('weixin_users')->where('username',$accountname)->value('password');
	if ($accountpsd == $res)	{
	    return response()->json(array(
                        'res' => 1
                ));
	}else {
	    return response()->json(array(
                        'res' => 0
                ));
	}
    }

    // wx.request post发送邮件
    public function email(Request $request) {
	$name = "河长制助手——微信小程序";
	$river_id = $request->input('river_id');
	$river_name = $request->input('river_name');
	$town_name = $request->input('town_name');
	$alarm_info = $request->input('alarm_info');
	$manager_name = $request->input('manager_name');
	$rece_email = $request->input('email');
	$res = Mail::send('emails.test',['river_id' => $river_id,'river_name' => $river_name,
			'town_name' => $town_name,'alarm_info' => $alarm_info,'manager_name' 					=> $manager_name],function ($message) {
	$to = '1451171391@qq.com';
	$message->to($to)->subject('河长制助手——微信小程序报警通知');
	});
	return response()->json(array(
	    	   'res' => 1
	));
    }
  
    // wx.request GET图片资源路径接口
    public function getImage() {
	$directory = "/app";
	$files = Storage::files($directory);
	return $files;		
    }
}	
